using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace OracleInstallHelper
{
    public partial class FMain : Form
    {
        private delegate void AppendOutputLineDelegate(string line);

        public FMain()
        {
            InitializeComponent();
        }

        private void FMain_Load(object sender, EventArgs e)
        {
            
        }

        private void FMain_Shown(object sender, EventArgs e)
        {
            string installationTypeText = string.Empty;

            try
            {
                bool showHelpMessage = false;

                string[] args = Environment.GetCommandLineArgs();

                if (args.Length == 2)
                {
                    string batchName = string.Empty;
                    string arguments = string.Empty;

                    string currentDir = Application.StartupPath;
                    string installDir = Path.Combine(currentDir, "..\\OracleInstallation");

                    if (args[1] == "/install")
                    {
                        installationTypeText = "Installation";
                        lblInformation.Text = string.Format(lblInformation.Text, installationTypeText);

                        batchName = Path.Combine(currentDir, "install.bat");
                        arguments = "odp.net20 \"" + installDir + "\" DatabaseManager true";

                        ProcessStartInfo startInfo = new ProcessStartInfo(batchName, arguments);
                        startInfo.UseShellExecute = false;
                        startInfo.CreateNoWindow = true;
                        startInfo.WorkingDirectory = currentDir;
                        startInfo.WindowStyle = ProcessWindowStyle.Hidden;

                        Process p = Process.Start(startInfo);
                        p.WaitForExit();
                    }
                    else if (args[1] == "/uninstall")
                    {
                        installationTypeText = "Deinstallation";
                        lblInformation.Text = string.Format(lblInformation.Text, installationTypeText);

                        batchName = Path.Combine(currentDir, "uninstall.bat");
                        arguments = "all DatabaseManager";

                        ProcessStartInfo startInfo = new ProcessStartInfo(batchName, arguments);
                        startInfo.UseShellExecute = false;
                        startInfo.CreateNoWindow = true;
                        startInfo.WorkingDirectory = currentDir;
                        startInfo.WindowStyle = ProcessWindowStyle.Hidden;

                        Process p = Process.Start(startInfo);
                        p.WaitForExit();

                        Directory.Delete(installDir, true);
                    }
                    else
                    {
                        showHelpMessage = true;
                    }
                }
                else
                {
                    showHelpMessage = true;
                }

                if (showHelpMessage)
                {
                    MessageBox.Show(
                        "Use /install or /uninstall as parameter!", 
                        "Parameters", 
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace,
                    "Fehler bei der " + installationTypeText + " des Oracle DataAccessClient",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }

            Close();
        }
    }
}